<?php
	include_once 'isLogin.php';
	$servicec=new service(); //服务类
	$id=$_GET['id'];
	$smarty->assign('id',$id);

	//读取一级分类
	$service_class=$servicec->service_class_list(' AND c.upid=0');
	$smarty->assign('service_class',$service_class['result']);

	//修改
	if($id!='' && $id!=null){
		$row=$servicec->service_info($id);
		$smarty->assign('row',$row);
	}
	

	if($_POST['submit']){
		//上传图片，生成缩略图
		$logo=$row['logo'];
		$qr_code=$row['qr_code'];

		//logo
		if($_FILES['logo']['name']!=''){
			//设置路径
			$folder = "../upload/service/";
			$annexFolder = $folder."large/";
			$smallFolder = $folder."small/";

			//删除旧图
			@unlink($annexFolder.$logo);
			@unlink($smallFolder.$logo);
		
			//上传新图
			$upimg = new uploadImg();
			$logo = $upimg->upLoad('logo',$annexFolder);
			//小图
			$upimg->smallImg($annexFolder.$logo,$smallFolder);
		}
		//二维码
		if($_FILES['qr_code']['name']!=''){
			//设置路径
			$folder = "../upload/service/";
			$annexFolder = $folder."large/";
			$smallFolder = $folder."small/";

			//删除旧图
			@unlink($annexFolder.$qr_code);
			@unlink($smallFolder.$qr_code);
		
			//上传新图
			$upimg = new uploadImg();
			$qr_code = $upimg->upLoad('qr_code',$annexFolder);
			//小图
			$upimg->smallImg($annexFolder.$qr_code,$smallFolder);
		}

		
		$arr= array(
					'class_id' =>$_POST['class_id'],//子时服务分类id
					'title' =>$_POST['title'], 		//服务标题
					'intro' =>$_POST['intro'],		//服务简介
					'logo' =>$logo,					//子时服务缩略图(logo)
					'linkman'=>$_POST['linkman'],	//联系人
					'phone'=>$_POST['phone'],		//联系电话
					'qr_code' =>$qr_code,
					'office_hours'=>$_POST['office_hours'],		//营业时间(文本）
					'province'=>$_POST['province'],	
					'city'=>$_POST['city'],	
					'county'=>$_POST['county'],	
					'address'=>$_POST['address'],			//详细地址
					'map_x'=>$_POST['map_x'],			//百度地图x坐标
					'map_y'=>$_POST['map_y'],			//百度地图y坐标
			 );



		if($id!='' && $id!=null){//update
			$check=$_POST['check'];
			if($check!='' && $check!=null){
				$arr['check']=$check;
			}

			$res=$servicec->service_update($id,$arr);	
		}else{//insert
			$arr['create_time']=time();
			$arr['check']='pass';

			$res=$servicec->service_add($arr);	
		}
		
		if($res){
			$msg="操作成功";
			$href="service_list.php";
		}else{
			$msg="操作失败";
			$href="service_list.php";
		}
		echo $commonc->submitAlert($msg,$href);
		exit;
	}


	//省市区
	$province = $commonc->getArea("province","",$row['province']);
	$smarty->assign("province",$province);
	if($row['city']){
		$city = $commonc->getArea("city",$row['province'],$row['city']);
		$smarty->assign("city",$city);	
	}
	if($row['county']){
		$county = $commonc->getArea("county",$row['city'],$row['county']);
		$smarty->assign("county",$county);	
	}
	//分类
	if($row['class_id']){
		//读取上级信息
		$class=$servicec->class_info($row['class_id']);
		$upid=$class['upid'];
		$smarty->assign("upid",$upid);	
		//读取当前二级全部
		$list2=$servicec->service_class_list(' AND c.upid='.$upid);
		$smarty->assign("list2",$list2['result']);	
	}



	$smarty->display("service_add.html");
?>